package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scm.entity.ScmInvoice;
import org.jeecg.modules.scm.service.IScmInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

 /**
 * @Description: scm_invoice
 * @Author: jeecg-boot
 * @Date:   2021-11-03
 * @Version: V1.0
 */
@Api(tags="发票管理")
@RestController
@RequestMapping("/invoice/scmInvoice")
@Slf4j
public class ScmInvoiceController extends JeecgController<ScmInvoice, IScmInvoiceService> {
	@Autowired
	private IScmInvoiceService scmInvoiceService;

	/**
	 * 分页列表查询
	 *
	 * @param scmInvoice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "scm_invoice-分页列表查询")
	@ApiOperation(value="scm_invoice-分页列表查询", notes="scm_invoice-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScmInvoice scmInvoice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ScmInvoice> queryWrapper = QueryGenerator.initQueryWrapper(scmInvoice, req.getParameterMap());
		Page<ScmInvoice> page = new Page<ScmInvoice>(pageNo, pageSize);
		IPage<ScmInvoice> pageList = scmInvoiceService.page(page, queryWrapper);
		System.out.println(pageList.getRecords());
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param scmInvoice
	 * @return
	 */
	@AutoLog(value = "scm_invoice-添加")
	@ApiOperation(value="scm_invoice-添加", notes="scm_invoice-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ScmInvoice scmInvoice) {
		scmInvoiceService.save(scmInvoice);
		return Result.OK(scmInvoice.getId());
	}

	/**
	 *  编辑
	 *
	 * @param scmInvoice
	 * @return
	 */
	@AutoLog(value = "scm_invoice-编辑")
	@ApiOperation(value="scm_invoice-编辑", notes="scm_invoice-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ScmInvoice scmInvoice) {
		scmInvoiceService.updateById(scmInvoice);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_invoice-通过id删除")
	@ApiOperation(value="scm_invoice-通过id删除", notes="scm_invoice-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		scmInvoiceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "scm_invoice-批量删除")
	@ApiOperation(value="scm_invoice-批量删除", notes="scm_invoice-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.scmInvoiceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "scm_invoice-批量查询")
	 @ApiOperation(value="scm_invoice-批量查询", notes="scm_invoice-批量查询")
	 @GetMapping(value = "/queryBatch")
	 public Result<?> queryBatch(@RequestParam(name="ids",required=true) String ids) {
//		 this.scmInvoiceService.removeByIds(Arrays.asList(ids.split(",")));
		 List<ScmInvoice> list = scmInvoiceService.queryBatch(ids);
		 return Result.OK(list);
	 }
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "通过id查询")
	@ApiOperation(value="通过id查询", notes="通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ScmInvoice scmInvoice = scmInvoiceService.getById(id);
		if(scmInvoice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(scmInvoice);
	}

	@ApiOperation(value = "上传附件", notes = "上传附件")
	@PostMapping("/uploadFile")
	public Result<?> uploadFile(@RequestParam String id,@RequestBody MultipartFile file) {
		String res = scmInvoiceService.uploadAccessory(id,file);
		if(res==null){
			Result.error("未找到数据");
		}
		return Result.OK(res);
	}
	 /**
	  * 	模糊查询发票
	  *
	  * */
	 @AutoLog(value = "模糊查询")
	 @ApiOperation(value="模糊查询", notes="模糊查询")
	 @GetMapping(value = "/fuzzyQuery")
	 public Result<?> fuzzyQuery(
			 @RequestParam(name="type",required=true) String type,
			 @RequestParam(name="value",required=true) String value,
			 @RequestParam(name="fields", required=false) String fields
	 )
	 {

		 List<ScmInvoice> result = scmInvoiceService.fuzzyQuery(type,value,fields);

		 if(result==null||result.isEmpty()) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(result);
	 }

	 /**
	  * OCR
	  * */
	 @ApiOperation(value = "OCR识别", notes = "OCR识别")
	 @PostMapping("/OCR")
	 public Result<?> uploadFile(@RequestBody MultipartFile file) {
		 Map<String, Object> ocr = scmInvoiceService.OCR(file);
		 return Result.OK(ocr);
	 }



    /**
    * 导出excel
    *
    * @param request
    * @param scmInvoice
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmInvoice scmInvoice) {
        return super.exportXls(request, scmInvoice, ScmInvoice.class, "scm_invoice");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmInvoice.class);
    }

}
